package com.ruoyi.system.service.impl;

import com.ruoyi.system.domain.Group;
import com.ruoyi.system.mapper.GroupMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.GroupService;
import org.springframework.stereotype.Service;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;

import javax.annotation.Resource;

/**
 * 小组信息管理表(Group)表服务实现类
 *
 * @author makejava
 * @since 2024-05-26 16:15:45
 */
@Service("groupService")
public class GroupServiceImpl implements GroupService {
    @Resource
    private GroupMapper groupMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param groupId 主键
     * @return 实例对象
     */
    @Override
    public Group queryById(Long groupId) {
        return this.groupMapper.queryById(groupId);
    }

    /**
     * 分页查询
     *
     * @param group       筛选条件
     * @param pageRequest 分页对象
     * @return 查询结果
     */
    @Override
    public Page<Group> queryByPage(Group group, PageRequest pageRequest) {
        long total = this.groupMapper.count(group);
        return new PageImpl<>(this.groupMapper.queryAllByLimit(group, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param group 实例对象
     * @return 实例对象
     */
    @Override
    public Group insert(Group group) {
        String s = sysUserMapper.selectNickNameByUserName(group.getCreateBy());
        this.groupMapper.insert(group);
        return group;
    }

    /**
     * 修改数据
     *
     * @param group 实例对象
     * @return 实例对象
     */
    @Override
    public Group update(Group group) {
        this.groupMapper.update(group);
        return this.queryById(group.getGroupId());
    }

    /**
     * 通过主键删除数据
     *
     * @param groupId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long groupId) {
        return this.groupMapper.deleteById(groupId) > 0;
    }
}
